package com.timothy.android.chart;

import com.timothy.android.chart.view.Bar2DVerView;
import com.timothy.android.config.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class Bar2DVerticalActivity extends BaseChartActivity {
	private final static String LOG_TAG = "Bar2DVerticalActivity";

	Bar2DVerView bar2DSfv;
//	SurfaceHolder sfh;
	private GestureDetector detector = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer_2d_ver_bar);
		
//		Bundle bundle = this.getIntent().getExtras();
//		String displayStyle = bundle.getString("displayStyle");
		
		//description for chart type and answer
		answerDesc = (TextView) this.findViewById(R.id.answerDesc);
		answerDesc.setText("Chart-Bar2D");
		
//		if("42".equals(type)){
			priorityList = (TextView) this.findViewById(R.id.priorityList);
			priorityList.setText("Choose Priority");
			priorityList.setVisibility(0);
			priorityList.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showWindow(v);
				}
			});
//		}
			
		bar2DSfv = (Bar2DVerView) this.findViewById(R.id.mySurfaceView);
//		sfh = mySfv.getHolder();

		bar2DSfv.setOnTouchListener(this);
		bar2DSfv.setLongClickable(true);
		bar2DSfv.setSaveEnabled(false);
		
		bar2DSfv.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
//				recoverBrightness();
				detector.onTouchEvent(arg1);
				return false;
			}
		});
		
		bar2DSfv.setBaseBean(baseBean);
		bar2DSfv.setPage(1);
		bar2DSfv.setScreenSize(SCREEN_SIZE);
		
		detector = new GestureDetector((OnGestureListener) this); 

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return detector.onTouchEvent(event);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Log.i(LOG_TAG, "onFling()...");
		int currentPage = bar2DSfv.getPage();
		
        if (e1.getX() - e2.getX() > Configuration.VERTICAL_MIN_DISTANCE && Math.abs(velocityX) > Configuration.MIN_VELOCITY) {
        	Log.i(LOG_TAG,"Move Left");
        	++currentPage;
        	if(currentPage<=baseBean.getTotalPages()){
        		bar2DSfv.setPage(currentPage);
        		bar2DSfv.DrawVertical2DBar(bar2DSfv.getHolder(), baseBean, currentPage, SCREEN_SIZE);
        	}
        } else if (e2.getX() - e1.getX() > Configuration.VERTICAL_MIN_DISTANCE && Math.abs(velocityX) > Configuration.MIN_VELOCITY) {
        	Log.i(LOG_TAG,"Move Right");
        	--currentPage;
        	if(currentPage>0){
        		bar2DSfv.setPage(currentPage);
        		bar2DSfv.DrawVertical2DBar(bar2DSfv.getHolder(), baseBean, currentPage, SCREEN_SIZE);
        	}
        }
		return false;
	}

}
